package com.lzs.baseruku.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.baseruku.service.LocatorService;
import com.lzs.baseruku.service.WarehouseService;
import com.lzs.entity.Locator;
import com.lzs.entity.Warehouse;
import com.lzs.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ymx
 * @since 2020-11-24
 */
@RestController
@RequestMapping("/baseruku/locator")
@Api(value = "库位管理文档")
public class LocatorController {
    @Autowired
    private LocatorService locatorService;
    @Autowired
    private WarehouseService warehouseService;


    @ApiOperation(value = "根据仓库和和类型查询库位不含原货位")
    @GetMapping("/selectLocatorByType2/{warehouseid}/{type}/{locatorcode}")
    public List<Locator> selectLocatorBycode2(@PathVariable Long warehouseid, @PathVariable String type,@PathVariable String locatorcode){
        QueryWrapper<Locator> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("warehouseid",warehouseid);
        queryWrapper.eq("type",type);
        queryWrapper.eq("state","0");
        queryWrapper.notIn("code",locatorcode);
        List<Locator> list = this.locatorService.list(queryWrapper);
        return list;
    }

    @ApiOperation(value = "下拉所有仓库")
    @GetMapping("/selectAllWarehouse")
    public R selectAllWarehouse(){
        List<Warehouse> warehouses = warehouseService.list();
        return R.ok().data("warehouses",warehouses);
    }

    @ApiOperation(value = "下拉所有所选仓库下的库位")
    @GetMapping("/selectWarehouseLocator2/{id}")
    public R selectWarehouseLocator(@PathVariable Integer id){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("warehouseid",id);
        List<Locator> locators = this.locatorService.list(queryWrapper);
        return R.ok().data("locators",locators);
    }


    @ApiOperation(value = "下拉所有所选仓库下的库位")
    @GetMapping("/selectWarehouseLocator")
    public R selectWarehouseLocator(){

        List<Locator> locators = this.locatorService.list();
        return R.ok().data("locators",locators);
    }

    @ApiOperation(value = "查询所有的库位")
    @PostMapping("/selectLocator/{page}/{limit}")
    public R selectLocator(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody Locator locator){
        Page<Locator> locatorPage = locatorService.selectLocator(page, limit, locator);
        return R.ok().data("locatorPage",locatorPage);
    }

    @ApiOperation(value = "根据仓库和和类型查询库位")
    @GetMapping("/selectLocatorByType/{warehouseid}/{type}")
    public List<Locator> selectLocatorBycode(@PathVariable Long warehouseid, @PathVariable String type){
        QueryWrapper<Locator> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("warehouseid",warehouseid);
        queryWrapper.eq("type",type);
        queryWrapper.eq("state","0");
        List<Locator> list = this.locatorService.list(queryWrapper);
        return list;
    }
    @ApiOperation(value = "根据货位编码查询库位信息")
    @GetMapping("/selectLocatorById1/{code}")
    public Locator selectLocatorBycode2(@PathVariable String code){
        QueryWrapper<Locator> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("code",code);
        Locator locator = this.locatorService.list(queryWrapper).get(0);
        return locator;
    }
    @ApiOperation(value = "根据ID查询库位")
    @GetMapping("/selectLocatorById/{id}")
    public R selectLocatorById(@PathVariable Long id){
        Locator locator = this.locatorService.getById(id);
        return R.ok().data("locator",locator);
    }

    @ApiOperation(value = "新增或修改库位")
    @PostMapping("/insertOrUpdateLocator")
    public R insertOrUpdateLocator(@RequestBody Locator locator){
            boolean b = this.locatorService.saveOrUpdate(locator);
            if (b){
                return R.ok().message("操作成功");
            }else {
                return R.error().message("操作失败");
            }
    }

    @ApiOperation(value = "根据ID删除库位")
    @DeleteMapping("/deleteLocator/{id}")
    public R deleteLocator(@PathVariable Long id){
        boolean b = this.locatorService.removeById(id);
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }



}

